package com.dylan.历史.util.mryx;

/**
 * @author : ankang
 * @date : 2019/12/6 15:36
 * @Description : 加密工具
 */
public class EncryptUtil {

    /**
     * RSA+AES混合加密后的解密
     * @param encryptedAesKey 被加密的AES秘钥
     * @param encryptedData 被加密的内容
     * @return 解密后内容
     */
    public static String decryptByRsaAndAes(String encryptedAesKey, String encryptedData) throws Exception {
        // 解密AES秘钥
        String decryptedAesKey = RSAUtil.base64AndDecrypt(encryptedAesKey);
        // 解密数据
        return AESClientUtils.aesDecryptString(encryptedData, decryptedAesKey);
    }

    /**
     * RSA+AES混合加密后的解密，包含gzip解压
     * @param encryptedAesKey 被加密的AES秘钥
     * @param encryptedData 被加密的内容
     * @return 解密后内容
     */
    public static String decryptByRsaAndAesWithGunzip(String encryptedAesKey, String encryptedData) throws Exception {
        // 解密数据
        String decryptData = decryptByRsaAndAes(encryptedAesKey, encryptedData);
        // 解压数据
        return ZipCompressUtils.gunzip(decryptData);
    }

    public static void main(String[] args) throws Exception {
//        testDecryptByRsaAndAes();
//        testDecryptByRsaAndAesWithGunzip();

        String encryptedData = "JtqaxgGFttB+Nfu0jWtw/gtxCUVSrgZ/xDtdknB4rk2mB+t9iQQrf7xVwKulpsOYjkv4KN9MXPiX\n" +
                "wBeRcYBfjOb5MFONRnbcaurr3FBSx2J8Thbe2ZO5epAbs4v/xL0zhNYhpYx4vbZRgRY24vz0W3hE\n" +
                "AWXn1hC5qjopmNLgnFo9W+tCs5axkglMX4SmNKrI";
        String encryptedAesKey = "Mk6S8UUphoWw3n8of8bhXNjh9nbuHjqL5tPkR5NUER7jPXOkcCv+A9k49OUuvR9LNQpu1K/q2T/M\n" +
                "AsrP+MWiKCOe5L55LGfzxA0odjTn10yHmySGgvqBb7SmLoU7vt2zaOjT3ptSw0IHhM6XPkwfWwGv\n" +
                "GIP++8C+AAj/Agn3YpI=";
        String data = null;
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            data = decryptByRsaAndAesWithGunzip(encryptedAesKey, encryptedData);
        }
        long end = System.currentTimeMillis();
        System.out.println("解密后数据:" + data + "，时间:" + (end - start));
    }

    private static void testDecryptByRsaAndAes() throws Exception {
        // 未加密的数据
        String unEncryptedData = "你看不到我，啦啦啦~~😋😋😋";
        // 未加密的AES秘钥
        String unEncryptedAeskey = "ABCDABCDABCDABCD";
        // 加密数据
        String encryptedData = AESClientUtils.aesEncryptString(unEncryptedData, unEncryptedAeskey);
        // 加密AES秘钥
        String encryptedAesKey = RSAUtil.encryptAndBase64(unEncryptedAeskey);
        System.out.println("加密后的数据:" + encryptedData);
        System.out.println("加密后的KEY:" + encryptedAesKey);

        // 解密AES秘钥
        String decryptedAesKey = RSAUtil.base64AndDecrypt(encryptedAesKey);
        // 解密数据
        String decryptedData = AESClientUtils.aesDecryptString(encryptedData, decryptedAesKey);
        System.out.println("解密后的数据:" + decryptedData);
    }

    private static void testDecryptByRsaAndAesWithGunzip() throws Exception {
        // 未加密的数据
        String unEncryptedData = "{\"userLng\":\"116.46892\",\"imei\":\"864371040494411\",\"userLat\":\"40.02198\",\"currentLng\":\"116.46892\",\"currentLat\":\"40.02198\"}";
        // 未加密的AES秘钥
        String unEncryptedAeskey = "ABCDABCDABCDABCD";
        // 压缩数据
        String unEncryptedGzipData = ZipCompressUtils.gzip(unEncryptedData);
        // 加密数据
        String encryptedData = AESClientUtils.aesEncryptString(unEncryptedGzipData, unEncryptedAeskey);
        // 加密AES秘钥
        String encryptedAesKey = RSAUtil.encryptAndBase64(unEncryptedAeskey);
        System.out.println("加密后的数据:" + encryptedData);
        System.out.println("加密后的KEY:" + encryptedAesKey);

        // 解密AES秘钥
        String decryptedAesKey = RSAUtil.base64AndDecrypt(encryptedAesKey);
        // 解密数据
        String decryptedData = AESClientUtils.aesDecryptString(encryptedData, decryptedAesKey);
        // 解压数据
        String decryptedGunzipData = ZipCompressUtils.gunzip(decryptedData);
        System.out.println("解密后的数据:" + decryptedGunzipData);
    }
}
